<template>
  <base-dialog
    :title="title + '用户'"
    ref="baseDialog"
    @handleClose="handleClose"
    @handleSubmit="handleSubmit"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="账号" prop="username">
        <el-input
          :disabled="title === '编辑'"
          v-model="form.username"
          placeholder="请输入账号"
          show-word-limit
          clearable
          :maxlength="20"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="nickname">
        <el-input
          v-model="form.nickname"
          placeholder="请输入姓名"
          show-word-limit
          clearable
          :maxlength="8"
        />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="form.sex" size="small">
              <el-radio-button label="M">男</el-radio-button>
              <el-radio-button label="W">女</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否锁定" prop="locked">
            <el-radio-group v-model="form.locked" size="small">
              <el-radio-button label="N">否</el-radio-button>
              <el-radio-button label="Y">是</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="部门名称" prop="deptId">
        <avue-input-tree
          v-model="form.deptId"
          placeholder="请选择所属部门"
          :node-click="getNodeData"
          :dic="treeListData"
          :props="defaultProps"
        ></avue-input-tree>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="form.email"
          placeholder="请输入邮箱"
          show-word-limit
          clearable
          :maxlength="200"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="form.mobile"
          placeholder="请输入手机号"
          show-word-limit
          clearable
          :maxlength="11"
        />
      </el-form-item>
    </el-form>
  </base-dialog>
</template>

<script>
import { add, edit } from "@/api/system/user"
import { roleList } from "@/api/system/role"
import { validatorNameCn, checkPhone, checkEmail } from "@/utils/validateRules"
export default {
  data() {
    return {
      title: "",
      form: {
        sex: "M",
        locked: "N"
      },
      rules: {
        username: [
          { validator: validatorNameCn, trigger: "blur" },
          { required: true, message: "请输入账号", trigger: "blur" }
        ],
        nickname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        locked: [{ required: true, message: "请选择锁定方式", trigger: "blur" }],
        deptId: [{ required: true, message: "请选择部门", trigger: "blur" }],
        mobile: [{ required: false, validator: checkPhone, trigger: "blur" }],
        email: [{ required: false, validator: checkEmail, trigger: "blur" }]
      },
      defaultProps: {
        label: "deptName",
        value: "id"
      },
      roleProps: {
        label: "roleName",
        value: "id"
      },
      //部门树
      treeListData: [],
      //角色
      roleListData: []
    }
  },
  methods: {
    init(treeListData, item) {
      this.treeListData = treeListData
      this.$refs.baseDialog.dialogVisible = true
      if (item) {
        this.title = "编辑"
        this.form = JSON.parse(JSON.stringify(item))
      } else {
        this.title = "新增"
      }
      roleList().then((res) => {
        this.roleListData = res.data
      })
    },
    handleClose() {
      this.form = {
        sex: "M",
        locked: "N"
      }
      this.title = ""
      this.$refs.baseDialog.dialogVisible = false
    },
    getNodeData(val) {
      console.log(val)
      // this.form.deptName = val.name
    },
    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.title === "编辑") {
            await edit(this.form)
            this.$message({
              type: "success",
              message: "用户编辑成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          } else {
            await add(this.form)
            this.$message({
              type: "success",
              message: "用户新增成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .disUoloadSty .el-upload--picture-card {
  display: none; /* 上传按钮隐藏 */
}
.el-select {
  width: 100%;
}
</style>
